package com.abe.abetest.alarm;

import android.app.AlarmManager;
import android.app.PendingIntent;
import android.content.Context;
import android.content.Intent;
import android.os.Build;
import android.os.Bundle;

import com.abe.abetest.utils.CommonUtils;

import java.util.Calendar;

import static android.content.Context.ALARM_SERVICE;

public class AlarmHelper {
    /**
     * @param id           id
     * @param count        重复次数
     * @param time         开始时间（在多长时间之后闹铃第一次提醒）单位秒
     * @param intervalTime 闹铃间隔时间单位秒
     * @param path         页面路径
     * @param json         页面数据
     */
    public static void setAlarm(Context context, int id, int count, int time, int intervalTime, String path, String json) {
        CommonUtils.wf("setAlarm:id(%d)--count(%d)--time(%d)--intervalTime(%d)--path(%s)--json(%s)", id, count, time, intervalTime, path, json);
        if (count <= 0) return;
        AlarmManager am = (AlarmManager) context.getSystemService(ALARM_SERVICE);
        int version = Build.VERSION.SDK_INT;
        Intent intent = new Intent(context, AlarmService.class);
        Bundle bundle = new Bundle();
        bundle.putString("path", path);
        bundle.putString("data", json);
        intent.putExtras(bundle);
        Calendar calendar = Calendar.getInstance();
        calendar.setTimeInMillis(System.currentTimeMillis());
        calendar.add(Calendar.SECOND, time);
        int type = AlarmManager.RTC_WAKEUP;
        if (count == 1) {
            intent.putExtra("needRepeating", false);
            PendingIntent pendingIntent = pendingIntent(context, id, intent);
            if (version >= Build.VERSION_CODES.M) {//6.0及其以上
                am.setExactAndAllowWhileIdle(type, calendar.getTimeInMillis(), pendingIntent);
            } else if (version >= Build.VERSION_CODES.KITKAT) {//4.4、5.0
                am.setExact(type, calendar.getTimeInMillis(), pendingIntent);
            } else {
                am.set(type, calendar.getTimeInMillis(), pendingIntent);
            }
        } else {
            intent.putExtra("id", id);
            intent.putExtra("count", count - 1);
            intent.putExtra("time", intervalTime);
            intent.putExtra("intervalTime", intervalTime);
            intent.putExtra("needRepeating", true);
            PendingIntent pendingIntent = pendingIntent(context, id, intent);
            if (version >= Build.VERSION_CODES.M) {//6.0及其以上
                am.setExactAndAllowWhileIdle(type, calendar.getTimeInMillis(), pendingIntent);
            } else if (version >= Build.VERSION_CODES.KITKAT) {//4.4、5.0
                am.setExact(type, calendar.getTimeInMillis(), pendingIntent);
            } else {
                am.set(type, calendar.getTimeInMillis(), pendingIntent);
            }
        }
    }

    public static void deleteAlarm(Context context, int id) {
        AlarmManager am = (AlarmManager) context.getSystemService(ALARM_SERVICE);
        Intent intent = new Intent(context, AlarmService.class);
        PendingIntent pendingIntent = pendingIntent(context, id, intent);
        am.cancel(pendingIntent);
    }

    private static PendingIntent pendingIntent(Context context, int id, Intent intent) {
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
            CommonUtils.log("pendingIntent1");
            return PendingIntent.getForegroundService(context, id, intent, PendingIntent.FLAG_CANCEL_CURRENT);
        }
        CommonUtils.log("pendingIntent2");
        return PendingIntent.getService(context, id, intent, PendingIntent.FLAG_CANCEL_CURRENT);
    }
}